package server;

import java.io.*;

public class LagouServlet extends HttpServlet {
    @Override
    public void doGet(Request request, Response response) {
        try (InputStream in = LagouServlet.class.getResourceAsStream("/index.html");
             ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            in.transferTo(out);
            String content = new String(out.toByteArray(), "utf-8");
            response.output((HttpProtocolUtil.getHttpHeader200(content.getBytes().length) + content));
        } catch (Exception e) {
            try {
                response.output((HttpProtocolUtil.getHttpHeader200(e.toString().getBytes().length) + e.toString()));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    @Override
    public void doPost(Request request, Response response) {
        String content = "<h1>LagouServlet post</h1>";
        try {
            response.output((HttpProtocolUtil.getHttpHeader200(content.getBytes().length) + content));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void init() throws Exception {

    }

    @Override
    public void destory() throws Exception {

    }
}
